import { APlugin } from '../types/common.interface';
import { TableDatas } from '../cors/table-datas';
import { Injectable, PluginInjectable } from '../decorators/inject';
import { Config } from '../cors/config';
import './col-width.plugin.less';
import { MergeCell } from '../cors/merge-cell';
import { RenderTrigger } from '../cors/render-trigger';
import { TCellControl } from '../types/common.interface'
import { objTransformToClass } from '../decorators/toJSON';
export interface ICellValue {
    col: number;
    font?: {[key: string]: any};
    border?: any;
    fill?: any;
    value?: string;
    type?: string;
    playload?: any;
}
export interface IRowValue {
    row: number;
    height?: number;
    cells: ICellValue[]
}
export interface ITableValues {
    mergeCells: { row: number; col: number; rowspan: number; colspan: number}[];
    colConf: {col: number, width: number}[];
    fullCtrl?: TCellControl[];
    datas: IRowValue[],
    config?: any;
}
@Injectable()
@PluginInjectable('DataHandlePlugin')
export class DataHandlePlugin extends APlugin {
    constructor(
        protected conf: Config, 
        protected tableData: TableDatas, 
        protected mergeCell: MergeCell, 
        protected renderTrigger: RenderTrigger
        ) {
        super();
    }
    public init() {
        
    }
    public destroy() {

    }
    public clear() {

    }
    public setData(data: ITableValues) {
        if(data.config)
        this.mergeCell.setMergedCells(data.mergeCells);
        if(data.fullCtrl) {
            // this.conf.cellControls = data.fullCtrl;
            this.conf.cellControls.push(...data.fullCtrl);
        }
        if(data.config) {
            objTransformToClass(this.conf, data.config)
        }
        for(let row of data.datas) {
            let rowInstance = this.tableData.getRow(row.row);
            if(row.height) {
                rowInstance.setHeight(row.height);
            }
            for(let cell of row.cells) {
                const cellInstance = rowInstance.getCell(cell.col);
                objTransformToClass(cellInstance, cell);
                // if(cell.fill) {
                //     cellInstance.fill = cell.fill;
                // }
                // if(cell.value) {
                //     cellInstance.value = cell.value;
                // }
                // if(cell.font) {
                //     cellInstance.font = cell.font;
                // }
                // if(cell.border) {
                //     cellInstance.border = cell.border;
                // }
                rowInstance.setCell(cell.col, cellInstance);
            }
            this.tableData.setRow(rowInstance, row.row);
        }
        for(let colConf of data.colConf) {
            const colInstance = this.tableData.getWholeCol(colConf.col);
            colInstance.setWidth(colConf.width);
            this.tableData.setWholeCol(colInstance, colConf.col);
        }
        this.renderTrigger.triggerRenderAll();
    }
}